package com.heimo.agentlicence.http;

import org.apache.http.HttpVersion;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

public class HttpClientFactory {

	private static DefaultHttpClient client;

	public synchronized static DefaultHttpClient getThreadSafeClient() {
		if (client != null) {
			return client;
		} else {
			client = new DefaultHttpClient();
			ClientConnectionManager mgr = client.getConnectionManager();
			HttpParams params = client.getParams();
			HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			client = new DefaultHttpClient(new ThreadSafeClientConnManager(
					params, mgr.getSchemeRegistry()), params);
			return client;
		}
	}
	
	public synchronized static void forceDestory() {
		if (client != null) {
			client.getConnectionManager().shutdown(); 
		}
	}
}
